package com.xiaohe66.web.application.birth;

import com.xiaohe66.common.util.IdWorker;
import com.xiaohe66.common.web.sec.annotation.NeedLogin;
import com.xiaohe66.web.application.birth.bo.BirthdayListBo;
import com.xiaohe66.web.application.birth.bo.BirthdaySaveBo;
import com.xiaohe66.web.application.birth.convert.BirthdayBoConverter;
import com.xiaohe66.web.application.birth.result.BirthdayResult;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.birth.agg.Birthday;
import com.xiaohe66.web.domain.birth.repository.BirthdayRepository;
import com.xiaohe66.web.domain.birth.service.BirthdayService;
import com.xiaohe66.web.domain.birth.value.BirthdayId;
import com.xiaohe66.web.domain.sys.sec.service.SecurityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaohe
 * @since 2023.07.13 11:57
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BirthdayAppService {

    private final BirthdayBoConverter boConverter;
    private final BirthdayRepository birthdayRepository;
    private final BirthdayService birthdayService;
    private final SecurityService securityService;

    @NeedLogin
    public BirthdayResult save(BirthdaySaveBo bo) {

        AccountId currentAccountId = securityService.getCurrentAccountId();

        long id = bo.getId() == null ? IdWorker.genId() : bo.getId();

        Birthday birthday = boConverter.toAgg(bo, new BirthdayId(id), currentAccountId);

        birthdayRepository.save(birthday);

        return boConverter.toResult(birthday);
    }

    @NeedLogin
    public void delete(Long id) {
        birthdayService.deleteById(new BirthdayId(id));
    }

    @NeedLogin
    public List<BirthdayResult> list(BirthdayListBo bo) {

        AccountId currentAccountId = securityService.getCurrentAccountId();

        List<Birthday> list = birthdayService.list(currentAccountId, bo.toPaging());

        return boConverter.toResult(list);
    }
}
